package com.mr.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import com.mr.model.BaseInfo;
import com.mr.model.Company;
import com.mr.model.Discount;
import com.mr.model.Goods;
import com.mr.model.GoodsOrder;
import com.mr.model.Member;
import com.mr.model.News;
import com.mr.model.Plan;
import com.mr.model.Point;
import com.mr.model.Proxy;
import com.mr.model.QQInfo;
import com.mr.model.SearchCommand;
import com.mr.model.Shop;
import com.mr.model.User;
import com.mr.model.Video;
import com.mr.model.WillCS;
import com.mr.service.CommonService;

@Service("commonService")
public class CommonServiceImpl extends BaseServiceImpl implements CommonService {

	public BaseInfo getBaseInfo(Long memberid) {
		BaseInfo baseInfo = baseDao.getObject(User.class, memberid);
		if(baseInfo==null){
			baseInfo = baseDao.getObject(Shop.class, memberid);
		}
		if(baseInfo==null){
			baseInfo = baseDao.getObject(Proxy.class, memberid);
		}
		if(baseInfo==null){
			baseInfo = baseDao.getObject(Company.class, memberid);
		}
		return baseInfo;
	}

	public Integer getUserCount(Long csid, SearchCommand command, String status) {
		DetachedCriteria qry = DetachedCriteria.forClass(User.class);
		qry.add(Restrictions.eq("csid", csid));
		if(StringUtils.isNotBlank(command.name)) qry.add(Restrictions.eq("name", command.name));
		if(StringUtils.isNotBlank(status)) qry.add(Restrictions.eq("status", status));
		qry.setProjection(Projections.rowCount());
		List list = hibernateTemplate.findByCriteria(qry);
		return Integer.valueOf(list.get(0)+"");
	}

	public List<User> getUserList(Long csid, SearchCommand command, String status, Integer from, Integer maxnum) {
		DetachedCriteria qry = DetachedCriteria.forClass(User.class);
		if(csid!=null) qry.add(Restrictions.eq("csid", csid));
		if(StringUtils.isNotBlank(command.name)) qry.add(Restrictions.eq("name", command.name));
		if(StringUtils.isNotBlank(status)) qry.add(Restrictions.eq("status", status));
		qry.addOrder(Order.desc("addtime"));
		List<User> userList = hibernateTemplate.findByCriteria(qry, from, maxnum);
		initUserList(userList);
		return userList;
	}

	public Integer getDiscountCount(Long relatedid, String name, Date startdate,
			Date enddate, String status) {
		DetachedCriteria qry = DetachedCriteria.forClass(Discount.class);
		qry.add(Restrictions.eq("relatedid", relatedid));
		if(StringUtils.isNotBlank(name)) qry.add(Restrictions.ilike("name", name, MatchMode.ANYWHERE));
		if(startdate!=null) qry.add(Restrictions.ge("startdate", startdate));
		if(enddate!=null) qry.add(Restrictions.le("enddate", enddate));
		if(StringUtils.isNotBlank(status)) qry.add(Restrictions.eq("status", status));
		qry.setProjection(Projections.rowCount());
		List qryList = hibernateTemplate.findByCriteria(qry);
		return Integer.valueOf(qryList.get(0)+"");
	}
	public Integer getDiscountCount(BaseInfo base, String name, Date startdate,
			Date enddate, String status) {
		Member member = baseDao.getObject(Member.class, base.getId());
		DetachedCriteria qry = DetachedCriteria.forClass(Discount.class);
		qry.add(Restrictions.or(Restrictions.eq("relatedid", member.getId()), Restrictions.eq("relatedid", member.getParentid())));
		if(StringUtils.isNotBlank(name)) qry.add(Restrictions.ilike("name", name, MatchMode.ANYWHERE));
		if(startdate!=null) qry.add(Restrictions.ge("startdate", startdate));
		if(enddate!=null) qry.add(Restrictions.le("enddate", enddate));
		if(StringUtils.isNotBlank(status)) qry.add(Restrictions.eq("status", status));
		qry.setProjection(Projections.rowCount());
		List qryList = hibernateTemplate.findByCriteria(qry);
		return Integer.valueOf(qryList.get(0)+"");
	}
	public List<Discount> getDiscountList(Long relatedid, String name,
			Date startdate, Date enddate, String status, Integer from, Integer maxnum) {
		DetachedCriteria qry = DetachedCriteria.forClass(Discount.class);
		qry.add(Restrictions.eq("relatedid", relatedid));
		if(StringUtils.isNotBlank(name)) qry.add(Restrictions.ilike("name", name, MatchMode.ANYWHERE));
		if(startdate!=null) qry.add(Restrictions.ge("startdate", startdate));
		if(enddate!=null) qry.add(Restrictions.le("enddate", enddate));
		if(StringUtils.isNotBlank(status)) qry.add(Restrictions.eq("status", status));
		qry.addOrder(Order.desc("addtime"));
		List<Discount> qryList = hibernateTemplate.findByCriteria(qry, from, maxnum);
		return qryList;
	}
	public List<Discount> getDiscountList(BaseInfo base, String name,
			Date startdate, Date enddate, String status, Integer from, Integer maxnum) {
		Member member = baseDao.getObject(Member.class, base.getId());
		DetachedCriteria qry = DetachedCriteria.forClass(Discount.class);
		qry.add(Restrictions.or(Restrictions.eq("relatedid", member.getId()), Restrictions.eq("relatedid", member.getParentid())));
		if(StringUtils.isNotBlank(name)) qry.add(Restrictions.ilike("name", name, MatchMode.ANYWHERE));
		if(startdate!=null) qry.add(Restrictions.ge("startdate", startdate));
		if(enddate!=null) qry.add(Restrictions.le("enddate", enddate));
		if(StringUtils.isNotBlank(status)) qry.add(Restrictions.eq("status", status));
		qry.addOrder(Order.desc("addtime"));
		List<Discount> qryList = hibernateTemplate.findByCriteria(qry, from, maxnum);
		return qryList;
	}
	public Integer getGoodsCount(Long relatedid, Long typeid, String name, String status) {
		DetachedCriteria qry = DetachedCriteria.forClass(Goods.class);
		qry.add(Restrictions.eq("relatedid", relatedid));
		if(typeid!=null) qry.add(Restrictions.eq("typeid", typeid));
		if(StringUtils.isNotBlank(name)) qry.add(Restrictions.ilike("name", name, MatchMode.ANYWHERE));
		if(StringUtils.isNotBlank(status)) qry.add(Restrictions.eq("status", status));
		qry.setProjection(Projections.rowCount());
		List qryList = hibernateTemplate.findByCriteria(qry);
		return Integer.valueOf(qryList.get(0)+"");
	}
	public Integer getGoodsCount(BaseInfo base, Long typeid, String name, String status) {
		Member member = baseDao.getObject(Member.class, base.getId());
		DetachedCriteria qry = DetachedCriteria.forClass(Goods.class);
		if(typeid!=null) qry.add(Restrictions.eq("typeid", typeid));
		qry.add(Restrictions.or(Restrictions.eq("relatedid", member.getId()), Restrictions.eq("relatedid", member.getParentid())));
		if(StringUtils.isNotBlank(name)) qry.add(Restrictions.ilike("name", name, MatchMode.ANYWHERE));
		if(StringUtils.isNotBlank(status)) qry.add(Restrictions.eq("status", status));
		qry.setProjection(Projections.rowCount());
		List qryList = hibernateTemplate.findByCriteria(qry);
		return Integer.valueOf(qryList.get(0)+"");
	}
	public List<Goods> getGoodsList(Long relatedid, Long typeid, String name, String status, Integer from, Integer maxnum) {
		DetachedCriteria qry = DetachedCriteria.forClass(Goods.class);
		if(typeid!=null) qry.add(Restrictions.eq("typeid", typeid));
		if(relatedid!=null)qry.add(Restrictions.eq("relatedid", relatedid));
		if(StringUtils.isNotBlank(name)) qry.add(Restrictions.ilike("name", name, MatchMode.ANYWHERE));
		if(StringUtils.isNotBlank(status)) qry.add(Restrictions.eq("status", status));
		qry.addOrder(Order.desc("addtime"));
		List<Goods> qryList = hibernateTemplate.findByCriteria(qry, from, maxnum);
		return qryList;
	}
	public List<Goods> getGoodsList(BaseInfo base,  Long typeid, String name, String status, Integer from, Integer maxnum) {
		Member member = baseDao.getObject(Member.class, base.getId());
		DetachedCriteria qry = DetachedCriteria.forClass(Goods.class);
		if(typeid!=null) qry.add(Restrictions.eq("typeid", typeid));
		qry.add(Restrictions.or(Restrictions.eq("relatedid", member.getId()), Restrictions.eq("relatedid", member.getParentid())));
		if(StringUtils.isNotBlank(name)) qry.add(Restrictions.ilike("name", name, MatchMode.ANYWHERE));
		if(StringUtils.isNotBlank(status)) qry.add(Restrictions.eq("status", status));
		qry.addOrder(Order.desc("addtime"));
		List<Goods> qryList = hibernateTemplate.findByCriteria(qry, from, maxnum);
		return qryList;
	}
	public Integer getNewsCount(Long relatedid, String name, String status) {
		DetachedCriteria qry = DetachedCriteria.forClass(News.class);
		qry.add(Restrictions.eq("relatedid", relatedid));
		if(StringUtils.isNotBlank(name)) qry.add(Restrictions.ilike("name", name, MatchMode.ANYWHERE));
		if(StringUtils.isNotBlank(status)) qry.add(Restrictions.eq("status", status));
		qry.setProjection(Projections.rowCount());
		List qryList = hibernateTemplate.findByCriteria(qry);
		return Integer.valueOf(qryList.get(0)+"");
	}
	public Integer getNewsCount(BaseInfo base, String name, String status) {
		Member member = baseDao.getObject(Member.class, base.getId());
		DetachedCriteria qry = DetachedCriteria.forClass(News.class);
		qry.add(Restrictions.or(Restrictions.eq("relatedid", member.getId()), Restrictions.eq("relatedid", member.getParentid())));
		if(StringUtils.isNotBlank(name)) qry.add(Restrictions.ilike("name", name, MatchMode.ANYWHERE));
		if(StringUtils.isNotBlank(status)) qry.add(Restrictions.eq("status", status));
		qry.setProjection(Projections.rowCount());
		List qryList = hibernateTemplate.findByCriteria(qry);
		return Integer.valueOf(qryList.get(0)+"");
	}
	public List<News> getNewsList(Long relatedid, String name, String status, Integer from, Integer maxnum) {
		DetachedCriteria qry = DetachedCriteria.forClass(News.class);
		qry.add(Restrictions.eq("relatedid", relatedid));
		if(StringUtils.isNotBlank(name)) qry.add(Restrictions.ilike("name", name, MatchMode.ANYWHERE));
		if(StringUtils.isNotBlank(status)) qry.add(Restrictions.eq("status", status));
		qry.addOrder(org.hibernate.criterion.Order.desc("addtime"));
		List<News> qryList = hibernateTemplate.findByCriteria(qry, from, maxnum);
		return qryList;
	}
	public List<News> getNewsList(BaseInfo base, String name, String status, Integer from, Integer maxnum) {
		Member member = baseDao.getObject(Member.class, base.getId());
		DetachedCriteria qry = DetachedCriteria.forClass(News.class);
		qry.add(Restrictions.or(Restrictions.eq("relatedid", member.getId()), Restrictions.eq("relatedid", member.getParentid())));
		if(StringUtils.isNotBlank(name)) qry.add(Restrictions.ilike("name", name, MatchMode.ANYWHERE));
		if(StringUtils.isNotBlank(status)) qry.add(Restrictions.eq("status", status));
		qry.addOrder(org.hibernate.criterion.Order.desc("addtime"));
		List<News> qryList = hibernateTemplate.findByCriteria(qry, from, maxnum);
		return qryList;
	}
	public Integer getOrderCount(Long memberid, Long goodsid, String name,
			String status) {
		DetachedCriteria qry = DetachedCriteria.forClass(GoodsOrder.class);
		if(memberid!=null) qry.add(Restrictions.eq("memberid", memberid));
		if(goodsid!=null) qry.add(Restrictions.eq("goodsid", goodsid));
		if(StringUtils.isNotBlank(name)) qry.add(Restrictions.ilike("name", name, MatchMode.ANYWHERE));
		if(StringUtils.isNotBlank(status)) qry.add(Restrictions.eq("status", status));
		qry.setProjection(Projections.rowCount());
		List qryList = hibernateTemplate.findByCriteria(qry);
		return Integer.valueOf(qryList.get(0)+"");
	}

	public List<GoodsOrder> getOrderList(Long memberid, Long goodsid, String name,
			String status, Integer from, Integer maxnum) {
		DetachedCriteria qry = DetachedCriteria.forClass(GoodsOrder.class);
		if(memberid!=null) qry.add(Restrictions.eq("memberid", memberid));
		if(goodsid!=null) qry.add(Restrictions.eq("goodsid", goodsid));
		if(StringUtils.isNotBlank(name)) qry.add(Restrictions.ilike("name", name, MatchMode.ANYWHERE));
		if(StringUtils.isNotBlank(status)) qry.add(Restrictions.eq("status", status));
		qry.addOrder(Order.desc("addtime"));
		List<GoodsOrder> qryList = hibernateTemplate.findByCriteria(qry, from, maxnum);
		return qryList;
	}

	public Integer getPointCount(Long memberid) {
		DetachedCriteria qry = DetachedCriteria.forClass(Point.class);
		qry.add(Restrictions.eq("memberid", memberid));
		qry.setProjection(Projections.rowCount());
		List qryList = hibernateTemplate.findByCriteria(qry);
		return Integer.valueOf(qryList.get(0)+"");
	}

	public List<Point> getPointList(Long memberid, Integer from, Integer maxnum) {
		DetachedCriteria qry = DetachedCriteria.forClass(Point.class);
		qry.add(Restrictions.eq("memberid", memberid));
		qry.addOrder(Order.desc("addtime"));
		List<Point> qryList = hibernateTemplate.findByCriteria(qry, from, maxnum);
		return qryList;
	}
	public void addPoint(Long userid, Long shopid, Integer point, String reason) {
		User user = baseDao.getObject(User.class, userid);
		Point p = new Point(userid, shopid, point, reason);
		baseDao.saveObject(p);
		user.addPoint(point);
		baseDao.saveObject(user);
	}
	public Integer getVideoCount(Long relatedid, String name, String status) {
		DetachedCriteria qry = DetachedCriteria.forClass(Video.class);
		if(relatedid!=null) qry.add(Restrictions.eq("relatedid", relatedid));
		if(StringUtils.isNotBlank(name)) qry.add(Restrictions.ilike("name", name, MatchMode.ANYWHERE));
		if(StringUtils.isNotBlank(status)) qry.add(Restrictions.eq("status", status));
		qry.setProjection(Projections.rowCount());
		List qryList = hibernateTemplate.findByCriteria(qry);
		return Integer.valueOf(qryList.get(0)+"");
	}

	public List<Video> getVideoList(Long relatedid, String name, String status, Integer from, Integer maxnum) {
		DetachedCriteria qry = DetachedCriteria.forClass(Video.class);
		if(relatedid!=null) qry.add(Restrictions.eq("relatedid", relatedid));
		if(StringUtils.isNotBlank(name)) qry.add(Restrictions.ilike("name", name, MatchMode.ANYWHERE));
		if(StringUtils.isNotBlank(status)) qry.add(Restrictions.eq("status", status));
		qry.addOrder(org.hibernate.criterion.Order.desc("addtime"));
		List<Video> qryList = hibernateTemplate.findByCriteria(qry, from, maxnum);
		return qryList;
	}

	public Integer getProxyCount(SearchCommand command) {
		DetachedCriteria qry = DetachedCriteria.forClass(Proxy.class);
		if(StringUtils.isNotBlank(command.name)) qry.add(Restrictions.ilike("name", command.name, MatchMode.ANYWHERE));
		if(StringUtils.isNotBlank(command.email)) qry.add(Restrictions.ilike("email", command.email, MatchMode.ANYWHERE));
		if(StringUtils.isNotBlank(command.tel)) qry.add(Restrictions.ilike("tel", command.tel, MatchMode.ANYWHERE));
		if(StringUtils.isNotBlank(command.address)) qry.add(Restrictions.ilike("address", command.address, MatchMode.ANYWHERE));
		if(StringUtils.isNotBlank(command.status)) qry.add(Restrictions.eq("status", command.status));
		qry.setProjection(Projections.rowCount());
		List qryList = hibernateTemplate.findByCriteria(qry);
		return Integer.valueOf(qryList.get(0)+"");
	}

	public List<Proxy> getProxyList(SearchCommand command, Integer from, Integer maxnum) {
		DetachedCriteria qry = DetachedCriteria.forClass(Proxy.class);
		if(StringUtils.isNotBlank(command.name)) qry.add(Restrictions.ilike("name", command.name, MatchMode.ANYWHERE));
		if(StringUtils.isNotBlank(command.email)) qry.add(Restrictions.ilike("email", command.email, MatchMode.ANYWHERE));
		if(StringUtils.isNotBlank(command.tel)) qry.add(Restrictions.ilike("tel", command.tel, MatchMode.ANYWHERE));
		if(StringUtils.isNotBlank(command.address)) qry.add(Restrictions.ilike("address", command.address, MatchMode.ANYWHERE));
		if(StringUtils.isNotBlank(command.status)) qry.add(Restrictions.eq("status", command.status));
		qry.addOrder(org.hibernate.criterion.Order.desc("addtime"));
		List<Proxy> qryList = hibernateTemplate.findByCriteria(qry, from, maxnum);
		initProxyList(qryList);
		return qryList;
	}

	public Integer getShopCount(Long proxyid, SearchCommand command, String status) {
		DetachedCriteria qry = DetachedCriteria.forClass(Shop.class);
		if(proxyid!=null) qry.add(Restrictions.eq("proxyid", proxyid));
		if(StringUtils.isNotBlank(command.name)) qry.add(Restrictions.ilike("name", command.name, MatchMode.ANYWHERE));
		if(StringUtils.isNotBlank(status)) qry.add(Restrictions.eq("status", status));
		qry.setProjection(Projections.rowCount());
		List qryList = hibernateTemplate.findByCriteria(qry);
		return Integer.valueOf(qryList.get(0)+"");
	}

	public List<Shop> getShopList(Long proxyid, SearchCommand command, String status, Integer from, Integer maxnum) {
		DetachedCriteria qry = DetachedCriteria.forClass(Shop.class);
		if(proxyid!=null) qry.add(Restrictions.eq("proxyid", proxyid));
		if(StringUtils.isNotBlank(command.name)) qry.add(Restrictions.ilike("name", command.name, MatchMode.ANYWHERE));
		if(StringUtils.isNotBlank(status)) qry.add(Restrictions.eq("status", status));
		qry.addOrder(org.hibernate.criterion.Order.desc("addtime"));
		List<Shop> qryList = hibernateTemplate.findByCriteria(qry, from, maxnum);
		initShopList(qryList);
		return qryList;
	}

	public List<QQInfo> getQQInfoList(Long relatedid) {
		DetachedCriteria qry = DetachedCriteria.forClass(QQInfo.class);
		if(relatedid!=null) qry.add(Restrictions.eq("relatedid", relatedid));
		qry.addOrder(org.hibernate.criterion.Order.desc("id"));
		List<QQInfo> qryList = hibernateTemplate.findByCriteria(qry);
		return qryList;
	}
	
	public Integer getPlanCount(Long relatedid, String name, String status) {
		DetachedCriteria qry = DetachedCriteria.forClass(Plan.class);
		if(relatedid!=null) qry.add(Restrictions.eq("relatedid", relatedid));
		if(StringUtils.isNotBlank(name)) qry.add(Restrictions.ilike("name", name, MatchMode.ANYWHERE));
		if(StringUtils.isNotBlank(status)) qry.add(Restrictions.eq("status", status));
		qry.setProjection(Projections.rowCount());
		List qryList = hibernateTemplate.findByCriteria(qry);
		return Integer.valueOf(qryList.get(0)+"");
	}

	public List<Plan> getPlanList(Long relatedid, String name, String status, Integer from, Integer maxnum) {
		DetachedCriteria qry = DetachedCriteria.forClass(Plan.class);
		if(relatedid!=null) qry.add(Restrictions.eq("relatedid", relatedid));
		if(StringUtils.isNotBlank(name)) qry.add(Restrictions.ilike("name", name, MatchMode.ANYWHERE));
		if(StringUtils.isNotBlank(status)) qry.add(Restrictions.eq("status", status));
		qry.addOrder(org.hibernate.criterion.Order.desc("addtime"));
		List<Plan> qryList = hibernateTemplate.findByCriteria(qry, from, maxnum);
		return qryList;
	}
	
	public Integer getWillCSCount(String willtype, Long relatedid, SearchCommand command) {
		DetachedCriteria qry = DetachedCriteria.forClass(WillCS.class);
		qry.add(Restrictions.eq("willtype", willtype));
		qry.add(Restrictions.eq("relatedid", relatedid));
		if(StringUtils.isNotBlank(command.name)) qry.add(Restrictions.ilike("name", command.name, MatchMode.ANYWHERE));
		if(StringUtils.isNotBlank(command.email)) qry.add(Restrictions.ilike("email", command.email, MatchMode.ANYWHERE));
		if(StringUtils.isNotBlank(command.tel)) qry.add(Restrictions.ilike("tel", command.tel, MatchMode.ANYWHERE));
		if(StringUtils.isNotBlank(command.address)) qry.add(Restrictions.ilike("address", command.address, MatchMode.ANYWHERE));
		qry.setProjection(Projections.rowCount());
		List qryList = hibernateTemplate.findByCriteria(qry);
		return Integer.valueOf(qryList.get(0)+"");
	}

	public List<WillCS> getWillCSList(String willtype, Long relatedid, SearchCommand command, Integer from, Integer maxnum) {
		DetachedCriteria qry = DetachedCriteria.forClass(WillCS.class);
		qry.add(Restrictions.eq("willtype", willtype));
		qry.add(Restrictions.eq("relatedid", relatedid));
		if(StringUtils.isNotBlank(command.name)) qry.add(Restrictions.ilike("name", command.name, MatchMode.ANYWHERE));
		if(StringUtils.isNotBlank(command.email)) qry.add(Restrictions.ilike("email", command.email, MatchMode.ANYWHERE));
		if(StringUtils.isNotBlank(command.tel)) qry.add(Restrictions.ilike("tel", command.tel, MatchMode.ANYWHERE));
		if(StringUtils.isNotBlank(command.address)) qry.add(Restrictions.ilike("address", command.address, MatchMode.ANYWHERE));
		qry.addOrder(org.hibernate.criterion.Order.desc("addtime"));
		List<WillCS> qryList = hibernateTemplate.findByCriteria(qry, from, maxnum);
		return qryList;
	}
	public void initCompanyList(List<Company> qryList){
		if(qryList==null || qryList.size()==0) return;
		for(BaseInfo base : qryList){
			Member member = baseDao.getObject(Member.class, base.getId());
			base.setMember(member);
		}
	}
	public void initProxyList(List<Proxy> qryList){
		if(qryList==null || qryList.size()==0) return;
		for(BaseInfo base : qryList){
			Member member = baseDao.getObject(Member.class, base.getId());
			base.setMember(member);
		}
	}
	public void initShopList(List<Shop> qryList){
		if(qryList==null || qryList.size()==0) return;
		for(BaseInfo base : qryList){
			Member member = baseDao.getObject(Member.class, base.getId());
			base.setMember(member);
		}
	}
	public void initUserList(List<User> qryList){
		if(qryList==null || qryList.size()==0) return;
		for(BaseInfo base : qryList){
			Member member = baseDao.getObject(Member.class, base.getId());
			base.setMember(member);
		}
	}
}
